

// import PIXI from './PIXI.js';
import Score from './Score.js';
import Timer from './Timer.js';
import Music from './Music.js';
let app = window.app;
export default class Game{
  constructor(width, height){
    this.width = width;
      this.height = height;
      this.vs = this.width/375;
      this.loaded = false;
      this.minTop = this.height - 170 * this.vs; //myz 减去红包高度
      this.canMove = false;
  }
  async loading(){
    console.log('load Game……')
    return new Promise((resolve, reject) => {
      window.app = new PIXI.Application({
        width: this.width,
        height: this.height,
        transparent: true,
      });
      document.body.appendChild(window.app.view);
      window.app.loader
      .add("bg","https://img.alicdn.com/imgextra/i4/826052692/O1CN01Ho8jJp1Vkyrj0QClq_!!826052692.png")
      .add("note1", "https://img.alicdn.com/imgextra/i3/826052692/O1CN01QyxlOF1VkyrnE5Rc2_!!826052692.png")
      .add("note2", "https://img.alicdn.com/imgextra/i4/826052692/O1CN01gUnKJc1VkyroblryF_!!826052692.png")
      .add("score", "https://img.alicdn.com/imgextra/i3/826052692/O1CN01OYihl21VkyrrurLDp_!!826052692.png")
      .add("timer", "https://img.alicdn.com/imgextra/i2/826052692/O1CN019Sl1xp1VkyreIqC20_!!826052692.png")
      .add("number2", "https://img.alicdn.com/imgextra/i4/826052692/O1CN01Fzwd291Vkyrvksgr5_!!826052692.png")
      .add("number1", "https://img.alicdn.com/imgextra/i3/826052692/O1CN01hK2gi41VkyrtPjjUn_!!826052692.png")
      .add("target", "https://img.alicdn.com/imgextra/i3/826052692/O1CN01911GsZ1VkyrtJEFvO_!!826052692.png")
      .load((loader, resources)=>{  
        this.loaded = true;  
        for(let p in resources){
          if(resources[p].error){
            this.loaded = false;
            break;
          }
        }
        window.resources = resources;
        app = window.app;
        resolve();
      });
    });
  }
  
  clean(){
    app.stage.removeChildren();
  }
  async load(){ 
    return await this.loading();
  }
  init(){ 
    app = window.app;
    //z-index
    //z-index 0
    this.addBg(); 
    this.addTarget();
    //music z-index 1
    this.music = new Music({
      width: this.width, 
      height: this.height, 
      beatHeight: this.height - 188 * this.vs + 55 * this.vs, 
      onMiss: ()=>{
        //console.log('miss')
        //this.score.update(0);
      },
      onEnd: ()=>{ 
        this.stop();
        setTimeout(()=>{
          this.onGameEnd && this.onGameEnd(this.score.score);
        }, 100)
      }
    });
     //app.stage.addChild(this.music);

    //z-index 2
    
    //z-index 3
    this.score = new Score(this.width, this.height);
    
    // app.stage.addChild(this.score);
    window.score=this.score;

    this.timer = new Timer(this.width, this.height);
    // app.stage.addChild(this.timer);
    window.timer=this.timer;


    
    //绑定事件
    app.stage.interactive = true;
    app.stage.on("pointerdown", (event) => {
      this.onTouchStart(event.data.global.x,event.data.global.y)
    })

    app.stage.on("pointermove", (event) => {
      this.onTouchMove(event.data.global.x,event.data.global.y)
    })

    app.stage.on("pointerup", (event) => {
      this.onTouchEnd(event.data.global.x,event.data.global.y)
    })


  }
  addBg(){
    const sprite = new PIXI.Sprite(window.resources["bg"].texture); 
    sprite.width = this.width;
    sprite.height = this.height;
    sprite.x = 0;
    sprite.y = 0;
    app.stage.addChild(sprite);
  }

  addTarget(){
    const sprite = new PIXI.Sprite(window.resources["target"].texture);
    sprite.width = 283 * this.vs;
    sprite.height = 188 * this.vs;
    sprite.x = 45 * this.vs;
    sprite.y = this.height - sprite.height;
    app.stage.addChild(sprite);
    this.$target=sprite;
    window.$target=sprite;
  }
  easeOutSine(pos){
    return Math.sin(pos * (Math.PI/2));
  }
  play(onGameEnd){  
    this.onGameEnd = onGameEnd;
    this.init();   
    this.music.play(); //myz 这里用来初始化掉落
    let i=30;
    setTimeout(()=>{
      app.ticker.add(this.run, this)
    }, 1000)
    app.gametimer = setInterval(() => {
      i--;
      if(i>=0){
        this.timer.update(i);
      }else{
        clearInterval(app.gametimer);
        app.gametimer = null;
        this.music.onEnd(); //这里包含了this.onGameEnd方法
      }
    }, 1000);
    
  }
  destroy(silent){
    //myz 如果退出页面时还存在计时器，判定为中断游戏
    if(app.gametimer){
      clearInterval(app.gametimer);
      app.gametimer = null;
      this.onGameEnd && this.onGameEnd(this.score.score,silent);
    }
    this.stop();
    Game.loaded = false;
  }
  stop(){
    app.ticker.remove(this.run, this)
    this.music && this.music.stop();
  }
  run(){
    const now = new Date().valueOf();
    this.music.dance(now);//这里是掉落金币
  }

  onTouchStart(x,y){
    //console.log('onTouchStart')
    if(!this.$target){
      this.canMove = false;
      return;
    }
    if(y < this.minTop){
      this.canMove = false;
      return;
    }
    let targetCenter = this.$target.x + this.$target.width/2;
    if(targetCenter-87>x || targetCenter+90<x){
      this.canMove = false;
    }else{
      this.canMove = true;
      this.leftX = x-this.$target.x;
    }
  }
  onTouchMove(x,y){
    //console.log('onTouchMove')
    if(!this.canMove) return;
    let targetCenter = x - this.leftX + this.$target.width/2;
    if(targetCenter-87<0 || targetCenter+90>this.width){
      //console.log('脱出屏幕区域',targetCenter-87,targetCenter+90,this.width,x)
    }else{
      this.$target.x = x - this.leftX;
    }
  }
  onTouchEnd(x,y){
    //console.log('onTouchEnd')
    return ;
  }
  
}